
package com.yimixian.app.goods;

import com.yimixian.app.R;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class IndicatorView extends FrameLayout {
    private int mTotalNum = -1;
    private LinearLayout mLinearLayout;
    private float mIndicatorPosition;
    private ImageView mIndicatorView;
    private int mIndicatorMargin;
    private int mIndicatorRadius;

    public IndicatorView(Context context) {
        super(context);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
    }

    //
    public void init(int totalNum) {
        if (totalNum != mTotalNum) {
            // init
            mTotalNum = totalNum;
            Resources mResources = getContext().getResources();
            mIndicatorMargin = mResources.getDimensionPixelSize(R.dimen.indicator_margin);
            mIndicatorRadius = mResources.getDimensionPixelSize(R.dimen.indicator_radius);

            if (mLinearLayout != null) {
                this.removeView(mLinearLayout);
            }
            mLinearLayout = new LinearLayout(getContext());
            mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

            for (int i = 0; i < totalNum; i++) {

                ImageView imageView = new ImageView(getContext());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        mIndicatorRadius, mIndicatorRadius);

                layoutParams.setMargins(mIndicatorMargin / 2, 0, mIndicatorMargin / 2, 0);

                imageView.setImageResource(R.drawable.indicator_gray_ball);
                mLinearLayout.addView(imageView, layoutParams);
            }
            FrameLayout.LayoutParams linearLayoutLayoutParams = new FrameLayout.
                    LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
            linearLayoutLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;

            addView(mLinearLayout, linearLayoutLayoutParams);

            if (mIndicatorView != null) {
                removeView(mIndicatorView);
            }

            ImageView indicatorView = new ImageView(getContext());
            FrameLayout.LayoutParams indicatorLayoutParams = new FrameLayout.
                    LayoutParams(mIndicatorRadius, mIndicatorRadius);
            mIndicatorView = indicatorView;

            indicatorView.setImageResource(R.drawable.indicator_yellow_ball);
            this.addView(indicatorView, indicatorLayoutParams);
        }
        scroll(0, 0);
    }

    public void scroll(int position, float positionOffset) {
        mIndicatorPosition = position + positionOffset;
        if (mIndicatorView != null) {
            float leftPadding = (mIndicatorMargin / 2.0f +
                    ((mIndicatorMargin + mIndicatorRadius) * (mIndicatorPosition)));
            mIndicatorView.setTranslationX(leftPadding);
        }
    }
}
